package com.zheng.shop.admin.controller.product;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zheng.common.base.BaseController;
import com.zheng.shop.common.Enum.ParamType;
import com.zheng.shop.common.constant.ShopResult;
import com.zheng.shop.common.constant.ShopResultConstant;
import com.zheng.shop.common.exception.ShopException;
import com.zheng.shop.dao.model.ShopParam;
import com.zheng.shop.rpc.api.ShopDictService;
import com.zheng.shop.rpc.api.ShopParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: xinqi
 * Date: 2017/11/24
 * Time: 10:25
 */
@Controller
@Api(value = "参数管理", description = "参数管理")
@RequestMapping("/manage/product/param")
public class ParamController extends BaseController {
    private static Logger _log = LoggerFactory.getLogger(ParamController.class);
    @Autowired
    private ShopParamService paramService;
    @Autowired
    private ShopDictService dictService;

    @ApiOperation(value = "参数首页")
    @RequiresPermissions("product:param:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        ParamType[] paramTypes = ParamType.values();
        model.addAttribute("paramTypes", paramTypes);
        return "/manage/product/param/index.jsp";
    }

    @ApiOperation(value = "参数列表")
    @RequiresPermissions("product:param:read")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(@RequestParam(required = false, defaultValue = "1", value = "pageNumber") int pageNumber,
                       @RequestParam(required = false, defaultValue = "10", value = "pageSize") int pageSize,
                       @RequestParam(required = false, value = "id") Integer productCategoryId) {
        Wrapper<ShopParam> wrapper = new EntityWrapper<>();
        if (null != productCategoryId) {
            wrapper.where("category_id=" + productCategoryId);
        } else {
            return null;
        }
        Page<ShopParam> page = paramService.selectPage(new Page<>(pageNumber, pageSize), wrapper);
        int total = paramService.selectCount(wrapper);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", page.getRecords());
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增属性")
    @RequiresPermissions("product:attribute:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model, Integer productCategoryId) {
        ParamType[] paramTypes = ParamType.values();
        model.addAttribute("paramTypes", paramTypes);

        model.addAttribute("productCategoryId", productCategoryId);

        return "/manage/product/param/create.jsp";
    }

    @ApiOperation(value = "新增参数")
    @RequiresPermissions("product:param:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(@RequestBody ShopParam param) {
        if (paramService.insert(param)) {
            return new ShopResult(ShopResultConstant.SUCCESS, ShopResultConstant.SUCCESS);
        }
        return new ShopResult(ShopResultConstant.FAILED, ShopResultConstant.FAILED);
    }

    @ApiOperation(value = "修改参数")
    @RequiresPermissions("product:param:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(Model model, @PathVariable("id") Integer id) {
        ParamType[] paramTypes = ParamType.values();
        model.addAttribute("paramTypes", paramTypes);

        ShopParam productParam = paramService.selectById(id);
        String paramJson = productParam.getParam();
        if (StringUtils.isNotEmpty(paramJson)) {
            model.addAttribute("paramJson", paramJson);
        }
        model.addAttribute("productParam", productParam);
        return "/manage/product/param/update.jsp";
    }

    @ApiOperation(value = "修改参数")
    @RequiresPermissions("product:param:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@RequestBody ShopParam param) {
        if (paramService.updateById(param)) {
            return new ShopResult(ShopResultConstant.SUCCESS, ShopResultConstant.SUCCESS);
        }
        return new ShopResult(ShopResultConstant.FAILED, ShopResultConstant.FAILED);
    }

    @ApiOperation(value = "删除属性")
    @RequiresPermissions("product:param:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@RequestBody List<Integer> ids) {
        if (paramService.deleteBatchIds(ids)) {
            return new ShopResult(ShopResultConstant.SUCCESS, ShopResultConstant.SUCCESS);
        }
        return new ShopResult(ShopResultConstant.FAILED, ShopResultConstant.FAILED);
    }

    @ApiOperation(value = "复制参数")
    @RequiresPermissions("product:param:create")
    @RequestMapping(value = "/copy", method = RequestMethod.GET)
    public String copy(Model model, String paramIdListStr) {
        JSONArray jsonArray = JSONArray.parseArray(paramIdListStr);
        List<Integer> paramIdList = jsonArray.toJavaList(Integer.class);

        EntityWrapper<ShopParam> paramEntityWrapper = new EntityWrapper<>();
        paramEntityWrapper.in("id", paramIdList);

        List<ShopParam> paramList = paramService.selectList(paramEntityWrapper);
        model.addAttribute("paramList", paramList);
        return "/manage/product/param/copy.jsp";
    }

    @ApiOperation(value = "复制参数")
    @RequiresPermissions("product:param:create")
    @RequestMapping(value = "/copy", method = RequestMethod.POST)
    @ResponseBody
    public Object copy(@RequestBody JSONObject param) {
        Integer productCategoryId = param.getInteger("productCategoryId");
        List<Integer> paramIds = param.getJSONArray("paramIds").toJavaList(Integer.class);
        try {
            paramService.copyParam(productCategoryId, paramIds);
        } catch (ShopException e) {
            return new ShopResult(ShopResultConstant.FAILED, e.getMessage());
        }
        return new ShopResult(ShopResultConstant.SUCCESS, ShopResultConstant.SUCCESS);
    }
}
